import pymysql

# 创建与数据库的连接
db = pymysql.connect(
    host="192.168.122.203",
    user="root",
    password="root",
    database="testdb",
    port=3307,  # 指定端口号
)

# 创建游标对象cursor
cursor = db.cursor()

# 插入sql语句
sql = "select * from student where age>=23"

try:
    # 执行sql语句
    cursor.execute(sql)
    results = cursor.fetchall()
    for row in results:
        sno = row[0]
        sname = row[1]
        sex = row[2]
        age = row[3]
        score = row[4]
        # 输出
        print("sno:", sno, "sname:", sname, "sex:", sex, "age:", age, "score:", score)
except Exception as e:
    print(e)
    print("查询失败")
finally:
    # 关闭数据库连接
    db.close()
